import requests
import json

# 直接硬编码API信息
API_KEY = "b682ab15b79740989665b1956252ebcf.PMwREp10RCyyqwJV"
API_URL = "https://open.bigmodel.cn/api/paas/v4/chat/completions"
MODEL_NAME = "GLM-4V-Flash"

# 准备请求头
headers = {
    "Authorization": f"Bearer {API_KEY}",
    "Content-Type": "application/json"
}

# 准备简单的文本请求
payload = {
    "model": MODEL_NAME,
    "messages": [
        {
            "role": "user",
            "content": [{"type": "text", "text": "你好，这是一个测试"}]
        }
    ],
    "temperature": 0.7,
    "max_tokens": 100
}

print("=== 准备发送请求 ===")
print(f"模型: {MODEL_NAME}")
print(f"API密钥长度: {len(API_KEY)} 字符")

# 发送请求
try:
    response = requests.post(
        API_URL,
        headers=headers,
        json=payload,
        timeout=60
    )
    
    print(f"\n响应状态码: {response.status_code}")
    print(f"响应内容: {response.text}")
    
    if response.status_code == 200:
        print("✅ 请求成功!")
    else:
        print(f"❌ 请求失败: {response.status_code}")
        # 分析可能的错误原因
        if response.status_code == 400:
            print(f"400错误详情: {response.text}")
            if "invalid_api_key" in response.text.lower():
                print("错误原因: API密钥无效或过期")
            elif "model_not_found" in response.text.lower():
                print("错误原因: 模型名称无效")
            elif "content" in response.text.lower():
                print("错误原因: 请求内容格式错误")
            elif "api_key" in response.text.lower():
                print("错误原因: API密钥相关错误")
            elif "authorization" in response.text.lower():
                print("错误原因: 授权相关错误")
        elif response.status_code == 401:
            print("错误原因: 未授权，请检查API密钥")
        elif response.status_code == 403:
            print("错误原因: 无权限访问该模型")
            
 except Exception as e:
    print(f"\n❌ 请求异常: {str(e)}")
    import traceback
    print(f"异常详情: {traceback.format_exc()}")